package com.xrui.hbase.conversion;

import com.xrui.hbase.Constants;
import com.xrui.hbase.HBaseCell;
import com.xrui.hbase.model.RecordFields;
import org.apache.hadoop.hbase.util.Bytes;

/**
 * Converts an HBase cell encoded as a Protocol Buffer.
 */
public class RawBytesCellConverter implements CellConverter {
    /* The name of this provider */
    public static final String PROVIDER_NAME = "RawBytesCellConverter";
    public static final String KEY = "value";

    /** {@inheritDoc} */
    @Override
    public RecordFields convert(HBaseCell cell) {
        byte[] value = (byte[]) cell.getData();
        RecordFields.Builder builder = RecordFields.Builder.newBuilder();
        builder.putField(Constants.ROW_KEY_FIELD_NAME, cell.getEntityId().toString());
        builder.putField(Constants.COLUMN_NAME_FIELD_NAME, cell.getColumn().toString());
        builder.putField(Constants.TIMESTAMP_FIELD_NAME, cell.getTimestamp());
        builder.putField(KEY, Bytes.toString(value));

        return builder.build();
    }

    /** {@inheritDoc} */
    @Override
    public String getName() {
        return PROVIDER_NAME;
    }
}
